home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / bbscfile.c < prev    next >
Text File  |  1990-07-30  |  10KB  |  342 lines

  1. /*
  2.     bbscfile.c
  3.  
  4. Mike Kelly
  5.  
  6.     06/12/83 v1.0   written
  7.     07/07/83 v1.0   updated
  8. */
  9.  
  10. /* #define DEBUG 1 */
  11.  
  12. #include "bbscdef.h"
  13.  
  14. #define LASTDATE  " 07/07/83 "
  15.  
  16. #define PGMNAME "BBSCFILE "
  17. #define VERSION " 1.0 "
  18.  
  19. hdrwrt()        /* write the header from memory variables */
  20.     {       /* header is a 1 record file */
  21.     int fd;
  22.     char    buf128[MSGSECT] ;
  23.  
  24.     if ((fd = open(HEADER,WRITE,0666)) < 0) /* open i/o */
  25.         {
  26.         portsout("Can't open header-file, will create it!") ;
  27.         portsout(CRLF) ;
  28.         if ((fd = creat(HEADER,0666)) < 0)
  29.             {
  30.             portsout("Can't create header-file, aborting!") ;
  31.             portsout(CRLF) ;
  32.             return(ERROR) ;
  33.             }
  34.         }
  35.     itoa(h_next_msg,h_next) ;   /* convert int to char */
  36.     strfill(buf128,26,MSGSECT) ;    /* init buf128 to all hex 1a */
  37.     sprintf(buf128,"%s~%s~",    /* build record */
  38.         h_next_msg,
  39.         h_date) ;
  40.     write(fd,buf128,MSGSECT) ;  /* write it */
  41.     close(fd) ;         /* no need to leave it open */
  42. #ifdef DEBUG
  43.     portsout(CRLF) ;
  44.     portsout("<<< header file written ok >>>") ;
  45.     portsout(CRLF) ;
  46. #endif
  47.     return(OK) ;
  48.     }
  49.  
  50. hdrread()       /* read the header file into memory */
  51.     {
  52.     int fd,
  53.         cnt;
  54.     char    buf128[MSGSECT];
  55.  
  56.     if ((fd = open(HEADER,READ,0666)) < 0)  /* open input */
  57.         {
  58.         portsout("Can't open header-file, using inital values!") ;
  59.         portsout(CRLF) ;
  60.         h_next = 1 ;
  61.         h_next_msg[0] = '1' ; h_next_msg[1] = 0 ;
  62.         h_date[0] = '0' ; h_date[1] = 0 ;
  63.         hdrwrt() ; return ;
  64.         }
  65.     if((cnt=read(fd,buf128,MSGSECT)) != MSGSECT)
  66.         {
  67.         portsout(CRLF) ;
  68.             portsout("<<< header read error >>>") ;
  69.         portsout(CRLF) ;
  70.         return(ERROR) ;
  71.         }
  72.     cnt = sscanf(buf128,"%[^~]~%[^~]~",
  73.             h_next_msg,
  74.             h_date) ;
  75.     close(fd) ;     /* no need to leave it open */
  76.  
  77. #ifdef DEBUG
  78.     portsout(CRLF) ;
  79.     portsout("<<< header read, next-message='") ;
  80.     portsout(h_next_msg) ;
  81.     portsout("', date='") ;
  82.     portsout(h_date) ;
  83.     portsout("'. >>>") ;
  84.     portsout(CRLF) ;
  85. #endif
  86.  
  87.     if (cnt != 2)
  88.         {
  89. #ifdef DEBUG
  90.     portsout(CRLF) ;
  91.     portsout("<<< Invalid header read! >>>") ;
  92.     portsout(CRLF) ;
  93. #endif
  94.         return(ERROR) ;
  95.         }
  96.     h_next = atoi(h_next_msg) ;
  97.     return(OK) ;
  98.     }
  99.  
  100. msgopen(how)
  101. int how ;       /* how to open 0=input, 1=output, 2=i/o */
  102.     {
  103.     int fd ;
  104.  
  105.     if ((fd = open(MESSAGES,how,0666)) < 0) /* open i/o */
  106.         {
  107.         portsout("can't open message-file, will create it!") ;
  108.         portsout(CRLF) ;
  109.         if ((fd = creat(MESSAGES,0666)) < 0)
  110.             {
  111.             portsout("can't create message-file, aborting!") ;
  112.             portsout(CRLF) ;
  113.             return(ERROR) ;
  114.             }
  115.         }
  116. #ifdef DEBUG
  117.     portsout(CRLF) ;
  118.     portsout("<<< message file opened ok >>>") ;
  119.     portsout(CRLF) ;
  120. #endif
  121.     return(fd) ;
  122.     }
  123.  
  124. msgclose(fd)
  125. int fd ;
  126.     {
  127. #ifdef DEBUG
  128.     portsout(CRLF) ;
  129.     portsout("<<< closing message file >>>") ;
  130.     portsout(CRLF) ;
  131. #endif
  132.     return(close(fd)) ;
  133.     }
  134.  
  135. msgwrt(fd)      /* write the message file from memory variables */
  136. int fd;     /* writes a message starting with the h_next msg # */
  137.     {
  138.     int rc,         /* return code */
  139.         cnt1,
  140.         cnt2,
  141.         len;
  142.     char    bufmsg0[MSG1MAX+1],
  143.         buf128[MSGSECT],
  144.         this1[10],
  145.         next1[10];
  146.  
  147.     rc = cnt1 = len = cnt2 = 0 ;
  148.     itoa(this1,h_next) ;                /* convert int to char */
  149.     rc = seek(fd,h_next - 1,0) ;        /* seek next available sector */
  150.     h_next++ ;
  151.     itoa(next1,h_next) ;
  152.     strfill(buf128,0,MSGSECT) ;     /* init buf128 to all hex 00 */
  153. /*
  154. *           build first piece of msg record
  155. */
  156.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",   /* build record */
  157.         this1,                  /* this rcd # */
  158.         next1,                  /*  points next rcd # */
  159.         msg_delete,             /* delete byte */
  160.         msg_date,
  161.         msg_time,
  162.         msg_to,
  163.         msg_from,
  164.         msg_pass,
  165.         msg_subject);
  166.     rc = write(fd,buf128,MSGSECT);  /* write the first 128 byte record */
  167.                     /*  for a message record */
  168. /*
  169. *           build the n+1 piece of msg record
  170. */
  171.  
  172.     len = (strlen(msg_text) / MSG1MAX) + 1; /* calc how many more 128 */
  173.                         /*  byte records to write */
  174.     cnt2 = 1 ;              /* init for substr */
  175.     while (len--)
  176.         {
  177.         itoa(this1,h_next);     /* calc/convert record #'s */
  178.         h_next++;
  179.         if (len == 0)
  180.             {
  181.             strcpy(next1,"0");  /* marks last 128 byte piece */
  182.             }           /*  of a msg */
  183.         else
  184.             {
  185.             itoa(next1,h_next);
  186.             }
  187.         strfill(bufmsg0,0,MSG1MAX);
  188.         substr(msg_text,bufmsg0,cnt2,MSG1MAX); /* mv MSG1MAX to buff */
  189.         cnt2 += MSG1MAX;        /* up cnt2 by MSG1MAX */
  190.         strfill(buf128,0,MSGSECT);  /* init buf128 to all hex 00 */
  191.         sprintf(buf128,"%s~%s~%s~%s~",
  192.             this1,          /* this rcd # */
  193.             next1,          /* point to next rcd # */
  194.             msg_delete,     /* delete byte */
  195.             bufmsg0);       /* piece of msg */
  196.         rc = write(fd,buf128,MSGSECT);  /* write n+1 128 byte record */
  197.         }
  198.  
  199.     strfill(buf128,26,MSGSECT);     /* fill with all hex 1a */
  200.     rc = write(fd,buf128,MSGSECT);  /* write all hex 1a 128 byte record */
  201.     return(OK);
  202.     }
  203.  
  204. msgrewrt(fd,r_msg)  /* re-write the message file from memory variables */
  205. int fd,     /* re-writes only the 1st part of a message */
  206.     r_msg;      /* used to update the delete byte */
  207.     {
  208.     int rc,         /* return code */
  209.         cnt1,
  210.         file_size;
  211.     char    buf128[MSGSECT],
  212.         this1[10],
  213.         next1[10];
  214.  
  215.     rc = cnt1 = 0;
  216.     if (r_msg > h_next) /* don't try to seek past end of file */
  217.         {
  218.         return(ERROR);
  219.         }
  220.     if ((rc = seek(fd,r_msg-1,0)) == ERROR) /* seek to requested sector */
  221.         {
  222.         return(ERROR);
  223.         }
  224.     itoa(this1,r_msg);      /* convert int to char */
  225.     r_msg++;
  226.     itoa(next1,r_msg);
  227.     strfill(buf128,0,MSGSECT);      /* init buf128 to all hex 00 */
  228. /*
  229. *           build first piece of msg record
  230. */
  231.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",   /* build record */
  232.         this1,                  /* this rcd # */
  233.         next1,                  /* points next rcd # */
  234.         msg_delete,             /* delete byte */
  235.         msg_date,
  236.         msg_time,
  237.         msg_to,
  238.         msg_from,
  239.         msg_pass,
  240.         msg_subject);
  241.     rc = write(fd,buf128,MSGSECT);  /* write the first 128 byte record */
  242.                     /*  for a message record */
  243.     return(OK);
  244.     }
  245.  
  246.  
  247. msgread(fd,msgno)       /* read message number requested */
  248. int fd,         /* returns ERROR if msg past eof */
  249.     msgno;          /* returns 0 if msg is not 1st piece */
  250.                 /*   of a message */
  251.                 /* returns 0 if msg is deleted */
  252.                 /* returns msg # if successful */
  253.     {
  254.     int rc,         /* return code */
  255.         cnt1,
  256.         cnt2,
  257.         len,
  258.         next,
  259.         ret_this,
  260.         file_size;
  261.     char    bufmsg0[MSG1MAX+1],
  262.         buf128[MSGSECT+256],
  263.         buftmp[MSGSECT+256],
  264.         this1[10],
  265.         next1[10];
  266.  
  267.     if (msgno > h_next)     /* don't try to seek past end of file */
  268.         {
  269.         return(ERROR);
  270.         }
  271.     if ((rc = seek(fd,msgno-1,0)) == ERROR)
  272.         {
  273.         portsout(CRLF) ;
  274.         portsout("Can't seek on message-file!") ;
  275.         portsout(CRLF) ;
  276.         return(ERROR);      /* when cant find it */
  277.         }
  278.     if (read(fd,buf128,MSGSECT) != MSGSECT) /* read 128 byte sector */
  279.         {
  280.         portsout(CRLF) ;
  281.         portsout("Can't read in message-file!") ;
  282.         portsout(CRLF) ;
  283.         return(ERROR);
  284.         }
  285. /*
  286. *           get first p